package com.esiag.isidis.marshaling;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MainReceiver {

	public static void main(String[] args) {

		 FileHandler fileHandler;
		  try {
			  fileHandler = new FileHandler("GhitahJ.log");

				final Logger logger = Logger.getLogger("toto");
				logger.addHandler( fileHandler);
				SimpleFormatter formatter = new SimpleFormatter();  
		        fileHandler.setFormatter(formatter);  
		byte[] b = new byte[1024];
		try{
			DatagramSocket serverSocket = new DatagramSocket(12345);
			logger.log(Level.INFO,"[Receiver] Receveur en marche ...");
			logger.log(Level.INFO,"[Receiver] en attente de Message ...");
//			System.out.println("[Receiver] Receveur en marche ...");
//
			//System.out.println("[Receiver] en attente de Message ...");

			DatagramPacket receivePacket = new DatagramPacket(b,b.length);
			serverSocket.receive(receivePacket);
		
			/*
			 * TODO: BY GHITAH
			 */
			//////////////////////////////////////////////////////////////
			String binary = new String(receivePacket.getData(), "UTF-8");
			logger.log(Level.INFO, "Binary reception: [" + binary.substring(0,9) + "]" );
			logger.log(Level.INFO, "Action {" + binary.substring(0, 7) + ", " + binary.substring(7, 9) + "}" );
			logger.log(Level.INFO, "Action " +"0"+ binary.substring(0,3)+ "," + binary.substring(3,7) + "," + "00" + binary.substring(7,9));
			logger.log(Level.INFO, "Action " + Integer.parseInt(binary.substring(0,3),2)+ "," + Integer.parseInt(binary.substring(3,7),2)+ "," + Integer.parseInt(binary.substring(7,9),2) );
			
		/*	System.out.println("Binary reception: [" + binary + "]" );
			System.out.println("Action {" + binary.substring(0, 7) + ", " + binary.substring(7, 9) + "}");
			System.out.println("Action " +"0"+ binary.substring(0,3)+ "," + binary.substring(3,7));
			System.out.println("Action " + Integer.parseInt(binary.substring(0,3),2)+ "," + Integer.parseInt(binary.substring(3,7),2));
			System.out.println("Action {" + Integer.parseInt(binary.substring(0, 7), 2) + ", " +Integer.parseInt(binary.substring(7, 9),2) + "}"); */
			//////////////////////////////////////////////////////////////

		}catch(Exception ex){
			System.out.println();
		}
	}
    catch(Exception e)
    {
    	
    }
}
}